<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商铺编号" prop="shopsId">
         <HouseInput v-model:type="queryParams.code"></HouseInput>
      </el-form-item>
      <el-form-item label="收费项目" prop="chargeitemId">
        <el-select v-model="queryParams.chargeitemId" placeholder="请选择收费项目" clearable>
          <el-option
            v-for="dict in chargeitemList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="费用开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择费用开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="费用结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择费用结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="付款状态" prop="paymentState">
        <el-select v-model="queryParams.paymentState" placeholder="请选择付款状态" clearable>
          <el-option
            v-for="dict in dict.type.zhwy_chargedata_payment_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['chargedata:chargedata:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['chargedata:chargedata:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['chargedata:chargedata:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['chargedata:chargedata:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="chargedataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="商铺编号" align="center" prop="shopsId">
        <template slot-scope="scope">
          <dict-tag-shop :options="shopsList" :value="scope.row.shopsId"/>
        </template>
      </el-table-column>
      <el-table-column label="收款人" align="center" prop="chargeUser" />
      <el-table-column label="收费项目" align="center" prop="chargeitemId">
        <template slot-scope="scope">
          <dict-tag-chargeitem :options="chargeitemList" :value="scope.row.chargeitemId"/>
        </template>
      </el-table-column>
      <el-table-column label="单价" align="center" prop="univalence" />
      <el-table-column label="费用开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="费用结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="催缴期限" align="center" prop="deadlineTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deadlineTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上次读取" align="center" prop="lastData" />
      <el-table-column label="本次读取" align="center" prop="thisData" />
      <el-table-column label="倍率" align="center" prop="rate" />
      <el-table-column label="损耗" align="center" prop="wastage" />
      <el-table-column label="数量" align="center" prop="number" />
      <el-table-column label="滞纳金" align="center" prop="latefee" />
      <el-table-column label="金额" align="center" prop="money" />
      <el-table-column label="实收金额" align="center" prop="fundsReceived" />
      <el-table-column label="付款状态" align="center" prop="paymentState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zhwy_chargedata_payment_state" :value="scope.row.paymentState"/>
        </template>
      </el-table-column>
      <el-table-column label="退款状态" align="center" prop="refundStatus" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zhwy_chargedata_refund_status" :value="scope.row.refundStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="退款总金额" align="center" prop="totalRefundAmount" />
      <el-table-column label="缴费单号" align="center" prop="billNumber" />
      <el-table-column label="缴费时间" align="center" prop="chargeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.chargeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="缴费方式" align="center" prop="payId">
        <template slot-scope="scope">
          <dict-tag-paymethod :options="paymentmethodList" :value="scope.row.payId"/>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['chargedata:chargedata:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['chargedata:chargedata:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改收费数据生成对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商铺编号" prop="shopsId">
          <HouseInput v-model:type="queryParams.code"></HouseInput>
          <el-button type="primary" @click="getName()">搜索</el-button>
        </el-form-item>
        <el-form-item label="收款人" prop="chargeUser">
          <el-input v-model="form.chargeUser" placeholder="请输入收款人姓名" />
        </el-form-item>
        <el-form-item label="收费项目" prop="chargeitemId">
          <el-select v-model="form.chargeitemId" placeholder="请选择收费项目">
            <el-option
              v-for="dict in chargeitemList"
              :key="dict.id"
              :label="dict.name"
              :value="parseInt(dict.id)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单价" prop="univalence">
          <el-input v-model="form.univalence" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="费用开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择费用开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="费用结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择费用结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="催缴期限" prop="deadlineTime">
          <el-date-picker clearable
            v-model="form.deadlineTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择催缴期限">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上次读取" prop="lastData">
          <el-input v-model="form.lastData" placeholder="请输入上次读取" />
        </el-form-item>
        <el-form-item label="本次读取" prop="thisData">
          <el-input v-model="form.thisData" placeholder="请输入本次读取" />
        </el-form-item>
        <el-form-item label="倍率" prop="rate">
          <el-input v-model="form.rate" placeholder="请输入倍率" />
        </el-form-item>
        <el-form-item label="损耗" prop="wastage">
          <el-input v-model="form.wastage" placeholder="请输入损耗" />
        </el-form-item>
        <el-form-item label="数量" prop="number">
          <el-input v-model="form.number" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="滞纳金" prop="latefee">
          <el-input v-model="form.latefee" placeholder="请输入滞纳金" />
        </el-form-item>
        <el-form-item label="金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="实收金额" prop="fundsReceived">
          <el-input v-model="form.fundsReceived" placeholder="请输入实收金额" />
        </el-form-item>
        <el-form-item label="退款总金额" prop="totalRefundAmount">
          <el-input v-model="form.totalRefundAmount" placeholder="请输入退款总金额" />
        </el-form-item>
        <el-form-item label="缴费单号" prop="billNumber">
          <el-input v-model="form.billNumber" placeholder="请输入缴费单号" />
        </el-form-item>
        <el-form-item label="缴费时间" prop="chargeTime">
          <el-date-picker clearable
            v-model="form.chargeTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择缴费时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="缴费方式" prop="payId">
          <el-select v-model="form.payId" placeholder="请选择缴费方式">
            <el-option
              v-for="dict in paymentmethodList"
              :key="dict.id"
              :label="dict.name"
              :value="parseInt(dict.id)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<!---->

<script>
import { listChargedata, getChargedata, delChargedata, addChargedata, updateChargedata } from "@/api/chargedata/chargedata";
import { listShops} from "@/api/shops/shops";
import { listChargeitem} from "@/api/charge/chargeitem";
import { listPaymentmethod} from "@/api/system/paymentmethod";
import { listRentalcontract} from "@/api/rentalcontract/rentalcontract";
import { getHirer} from "@/api/renter/hirer";

export default {
  name: "Chargedata",
  dicts: ['zhwy_chargedata_payment_state', 'sys_user_sex','zhwy_chargedata_refund_status'],

  data() {
    return {
      //支付方式
      paymentmethodList:[],
      //商铺数据列表
      shopsList: [],
      //收费项数据列表
      chargeitemList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收费数据生成表格数据
      chargedataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopsId: null,
        chargeitemId: null,
        startTime: null,
        endTime: null,
        paymentState: null,
        refundStatus: null,
        code: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //获取租户姓名
    getName(){
      listRentalcontract({shopsId:this.queryParams.shopsId}).then(resp=>{
           getHirer(resp.rows[0].hirerId).then(res=>{
              this.form.chargeUser = res.data.name
           })
      })
    },
    /** 查询收费数据生成列表 */
    getList() {
      this.loading = true;
      listChargedata(this.queryParams).then(response => {
        this.chargedataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listChargeitem(this.queryParams).then(response => {
        this.chargeitemList = response.rows;
      });
      listShops(this.queryParams).then(response => {
        this.shopsList = response.rows;
      });
      listPaymentmethod(this.queryParams).then(response => {
        this.paymentmethodList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        shopsId: null,
        chargeitemId: null,
        univalence: null,
        startTime: null,
        endTime: null,
        deadlineTime: null,
        lastData: null,
        thisData: null,
        rate: null,
        wastage: null,
        number: null,
        latefee: null,
        money: null,
        fundsReceived: null,
        paymentState: null,
        refundStatus: null,
        totalRefundAmount: null,
        billNumber: null,
        chargeTime: null,
        payId: null,
        chargeUser: null,
        code: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      listShops({code:this.queryParams.code}).then(resp=>{
        this.queryParams.code = resp.rows[0].code
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加收费数据生成";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getChargedata(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改收费数据生成";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateChargedata(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChargedata(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除收费数据生成编号为"' + ids + '"的数据项？').then(function() {
        return delChargedata(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('chargedata/chargedata/export', {
        ...this.queryParams
      }, `chargedata_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
